﻿using System;
using System.Text;

namespace SocketClient
{
    public class TCPpackage
    {
        public static byte[] wrap(string msg)
        {
            //构造内容
            byte[] bodyBytes = Encoding.UTF8.GetBytes(msg);
            //构造表头数据，固定4个字节的长度，表示内容的长度
            byte[] headerBytes = BitConverter.GetBytes(bodyBytes.Length);
 
            byte[] tempBytes = new byte[headerBytes.Length + bodyBytes.Length];
            ///拷贝到同一个byte[]数组中，发送出去..
            Buffer.BlockCopy(headerBytes, 0, tempBytes, 0, headerBytes.Length);
            Buffer.BlockCopy(bodyBytes, 0, tempBytes, headerBytes.Length, bodyBytes.Length);
            return tempBytes;
        }

    }
}